package com.syz.mall.controller;

import com.syz.mall.pojo.Category;
import com.syz.mall.pojo.Order;
import com.syz.mall.pojo.Product;
import com.syz.mall.pojo.User;
import com.syz.mall.pojo.vo.HomeProductVO;
import com.syz.mall.service.ICategoryService;
import com.syz.mall.service.IOrderService;
import com.syz.mall.service.IProductService;
import com.syz.mall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Controller
public class IndexController {
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IProductService productService;

    @Autowired
    private IOrderService orderService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IUserService userService;

    // http://localhost:8080/
    // http://localhost:8080
    @RequestMapping("/")
    public String index(Model model, HttpSession session) {
        User user= (User) session.getAttribute("user");
        model.addAttribute("user",user);
        //System.out.println("IndexController.index");
        List<Category> topCategoryList = redisTemplate.opsForList().range("topCategoryList", 0, -1);
        if (CollectionUtils.isEmpty(topCategoryList)) {
            topCategoryList = categoryService.selectTopCategoryList();
            //更新到缓存里面
            redisTemplate.opsForList().rightPushAll("topCategoryList", topCategoryList);
        }
        List<Category> secondCategoryList = redisTemplate.opsForList().range("secondCategoryList", 0, -1);
        if(CollectionUtils.isEmpty(secondCategoryList)){
            secondCategoryList = categoryService.selectSecondCategoryList();
            redisTemplate.opsForList().rightPushAll("secondCategoryList", secondCategoryList);
        }

        model.addAttribute("topCategoryList", topCategoryList);
        model.addAttribute("secondCategoryList", secondCategoryList);

        List<HomeProductVO> homeProductVOS=new ArrayList<>();
        for(Category topCat:topCategoryList){
            HomeProductVO homeProductVO=new HomeProductVO();
            homeProductVO.setTopId(topCat.getId());
            homeProductVO.setTopCategory(topCat.getName());
            List<Category> categories=new ArrayList<>();
            for(Category secCat:secondCategoryList){
                if(Objects.equals(secCat.getParentId(), topCat.getId())){
                    categories.add(secCat);
                }
            }
            homeProductVO.setCategories(categories);
            //设置商品
            List<Product> products=new ArrayList<>();
            products=productService.selectByTopLimit(topCat.getId(),8);
            homeProductVO.setProducts(products);
            homeProductVOS.add(homeProductVO);
        }
        model.addAttribute("homeData", homeProductVOS);

        List<Product> recommends=productService.selectLimit(6);
        model.addAttribute("recommends", recommends);
        return "index";
    }
    @RequestMapping("/getUserCenterPage")
    public String getUserCenterPage(){
        return "user_center_index";
    }
    @RequestMapping("/getOrderCenterPage")
    public String getOrderCenterPage(HttpSession session,Model model){
        User user= (User) session.getAttribute("user");
        List<Product> list=userService.selectUserCollect(user.getId());
        model.addAttribute("list",list);
        return "order_center_index";
    }
    @RequestMapping("/getServiceCenterPage")
    public String getServiceCenterPage(){
        return "service_center_index";
    }

    @RequestMapping("/getDiscountZone")
    public String getDiscountZone(Model model){
        List<Product> recommends=productService.selectLimit(8);
        model.addAttribute("recommends", recommends);
        List<Product> list=productService.selectByZone(3);
        model.addAttribute("list",list);
        return "zones/discount_zone";
    }
    @RequestMapping("/getFullReductionZone")
    public String getFullReductionZone(Model model){
        List<Product> recommends=productService.selectLimit(8);
        model.addAttribute("recommends", recommends);
        List<Product> list=productService.selectByZone(2);
        model.addAttribute("list",list);
        return "zones/full_reduction_zone";
    }
    @RequestMapping("/getHotZone")
    public String getHotZone(Model model){
        List<Product> recommends=productService.selectLimit(8);
        model.addAttribute("recommends", recommends);
        List<Product> list=productService.selectByZone(1);
        model.addAttribute("list",list);
        return "zones/hot_zone";
    }
    @RequestMapping("/getSearchProductPage")
    public String getSPP(String name,Model model){
        List<Product> list=productService.selectByName(name);
        model.addAttribute("list",list);
        model.addAttribute("searchName",name);
        List<Product> recommends=productService.selectLimit(8);
        model.addAttribute("recommends", recommends);
        return "/search_product";
    }
}
